

<template>
  <div class="app-container">
    <basic-container>
      <avue-crud
        ref="crud"
        v-model="form"
        :table-loading="loading"
        :page="page"
        :data="data"
        :option="tableOption"
        :permission="permissionList"
        :before-open="beforeOpen"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @size-change="sizeChange"
        @current-change="currentChange"
        @refresh-change="refreshChange"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @on-load="onLoad">

        <template slot="menuLeft">
          <el-button type="primary"
            icon="el-icon-upload"
            size="small"
            @click="handleImportTable"
            v-if="permissions.gen_generator_import"
          >导入</el-button>
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permissions.gen_generator_del"
                     @click="handleDelete">删 除
          </el-button>
        </template>

        <template
          slot-scope="scope"
          slot="menu">
          <el-button
            type="text"
            icon="el-icon-check"
            v-if="permissions.gen_generator_edit"
            @click="handleEditTable(scope.row)">编辑
          </el-button>
          <el-button
            type="text"
            icon="el-icon-check"
            v-if="permissions.gen_generator_preview"
            @click="handlePreview(scope.row)">预览
          </el-button>
          <el-button
            type="text"
            icon="el-icon-download"
            v-if="permissions.gen_generator_code"
            @click="handleGenCode(scope.row)">生成
          </el-button>
        </template>

      </avue-crud>

      <el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh">
        <el-tabs v-model="preview.activeName">
          <el-tab-pane
            v-for="(value, key) in preview.data"
            :label="key.substring(key.lastIndexOf('/')+1,key.indexOf('.vm'))"
            :name="key.substring(key.lastIndexOf('/')+1,key.indexOf('.vm'))"
            :key="key"
          >
            <pre>{{ value }}</pre>
          </el-tab-pane>
        </el-tabs>
      </el-dialog>


      <el-dialog title="导入表单" width="60%" :visible.sync="importTableVisible">
        <import-table ref="import" @getList="searchReset"></import-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="importTableVisible = false">取 消</el-button>
          <el-button type="primary" @click="importTableVisible = false">确 定</el-button>
        </div>
      </el-dialog>

    </basic-container>


  </div>

</template>

<script>
  import {addObj, getObj, previewTable, genCode, delObj, fetchList, putObj} from '@/api/gen/gen';
  import {tableOption} from '@/const/crud/gen/gen';
  import importTable from "./importTable";
  import {mapGetters} from "vuex";

  export default {
    components: {importTable},
    data() {
      return {
        data: [],
        importTableVisible: false,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        selectionList: [],
        form: {},
        query: {},
        loading: false,
        tableOption: tableOption,
        // 预览参数
        preview: {
          open: false,
          title: "代码预览",
          data: {},
          activeName: "model.java"
        }
      }
    },
    computed: {
      ...mapGetters(["permissions"]),
      permissionList() {
        return {
          delBtn: this.vaildData(this.permissions['gen_generator_del'], false),
          editBtn: this.vaildData(this.permissions['gen_generator_edit'], false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {

      beforeOpen: function (show) {
        this.form.password = undefined
        show()
      },

      onLoad(page, params) {
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.query)).then(response => {
          this.data = response.data.data.records
          this.page.total = response.data.data.total
        })
      },

      rowSave(row, done, loading) {
        addObj(this.form).then(() => {
          this.onLoad(this.page)
          done()
          this.$notify.success('创建成功')
        }).catch(() => {
          loading()
        })
      },

      rowUpdate(row, index, done, loading) {
        putObj(this.form).then(() => {
          this.onLoad(this.page)
          done()
          this.$notify.success('修改成功')
        }).catch(() => {
          loading()
        })
      },

      rowDel(row) {
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(() => {
          this.onLoad(this.page)
          this.$notify.success('删除成功')
        })
      },

      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return delObj(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },


      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      refreshChange() {
        this.onLoad(this.page,this.query)
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      handleEditTable(row) {
        const tableId = row.id;
        this.$router.push({ path: "/gen/edit", query: { tableId: tableId } });
      },
      /** 预览按钮 */
      handlePreview(row) {
        previewTable(row.id).then(response => {
          this.preview.data = response.data.data;
          this.preview.open = true;
        });
      },
      /** 生成代码操作 */
      handleGenCode(row) {
        const tableNames = row.tableName;
        if (tableNames == "") {
          this.$message.error("请选择要生成的数据");
          return;
        }

        genCode(tableNames).then(res => {
          if (res.data.code === 0) {
            this.$message.success("代码生成成功！");
            this.refreshChange();
          }

        }).catch(function() {});

      },

      handleImportTable() {
        this.importTableVisible = true;
        // this.$refs.import.show();
      },

    }
  };
</script>

<style lang="scss" scoped>
</style>

